package com.example.headlines.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.widget.Adapter;

import com.example.headlines.R;
import com.example.headlines.adapter.MyPagerAdapter;
import com.example.headlines.api.Api;
import com.example.headlines.api.ApiConfig;
import com.example.headlines.api.TtitCallback;
import com.example.headlines.entity.TabEntity;
import com.example.headlines.fragment.CollectFragment;
import com.example.headlines.fragment.HomeFragment;
import com.example.headlines.fragment.MyFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HomeActivity extends AppCompatActivity {

    private String[] mTitles = {"首页", "资讯", "我的"};
    private int[] mIconUnselectIds = {
            R.mipmap.home_unselect, R.mipmap.collect_unselect,
            R.mipmap.my_unselect};
    private int[] mIconSelectIds = {
            R.mipmap.home_selected, R.mipmap.collect_selected,
            R.mipmap.my_selected};
    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
    private ViewPager viewPager;
    private CommonTabLayout commonTabLayout;
    List<String> contents;
    List<String> contents1;
    List<String> contents2;
    List<String> contents3;
    MyPagerAdapter adapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        viewPager = findViewById(R.id.viewpager);
        commonTabLayout = findViewById(R.id.commonTabLayout);
        contents=home(1);
        contents1=home(2);
        contents2=home(3);
        contents3=home(4);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        mFragments.add(HomeFragment.newInstance());
        mFragments.add(CollectFragment.newInstance(contents,contents1,contents2,contents3));
        mFragments.add(MyFragment.newInstance());

        for (int i = 0; i < mTitles.length; i++) {
            mTabEntities.add(new TabEntity(mTitles[i], mIconSelectIds[i], mIconUnselectIds[i]));
        }
        commonTabLayout.setTabData(mTabEntities);
        commonTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {
            }
        });

        viewPager.setOffscreenPageLimit(mFragments.size());
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                commonTabLayout.setCurrentTab(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager(), mTitles, mFragments));

    }
    public List<String> home(int a) {
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("username", "account");
        params.put("password", "pwd");
        List<String> result = new ArrayList<>();
        Api.config(ApiConfig.LOGIN,params).getRequest(this,a, new TtitCallback() {
            @Override
            public void onSuccess(String res) {
                Log.e("onSuccess",res);
                try {
                    JSONObject jsonObject=new JSONObject(res);
                    Integer code = (Integer) jsonObject.get("code");
                    Log.i("code",code+"");
                    if (200 == code&&null!=jsonObject.get("data")) {
                        JSONObject data = jsonObject.getJSONObject("data");
                        JSONArray list = data.getJSONArray("list");
                        for (int i = 0; i < list.length(); i++) {
                            JSONObject jsonObject1 = list.getJSONObject(i);
//                            Log.i("content" + i, (String) jsonObject1.get("content"));
                            result.add((String) jsonObject1.get("content"));
                            result.add((String) jsonObject1.get("newTitle"));
                            result.add((String) jsonObject1.get("newTitleImgUrl"));

                        }
                        Log.i("aaaa", result.toString());

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(Exception e) {

            }
        });
        return result;
    }
}